<?php
namespace Home\Controller;
use Think\Controller;

/**
 * 当前项目api
 */
class SingleController extends HomeBasicController {

	public $Project = '';
	public $Module = '';
	public $Function ='';
	
	public function _initialize(){
		parent::_initialize();
		$this->checkLogin();
		$this->Project = D('Project');
		$this->Module = D('Module');
		$this->Function = D('Function');
	}

    public function index(){
        if(session('M_TYPE') == 2 && $_GET['p_id'] != 22){
            //如果是客户登陆防止其看到其他项目接口
            $where['p_id'] = $this->Project->where(array('own_m_id'=>session('M_ID')))->getField('p_id');
        }else{
            $where['p_id'] = $_GET['p_id'];
        }
    	$project_info = $this->Project->findProject($where);
    	//查询模块名称
    	$module_list = $this->Module->selectModule($where,'','');
    	//查询当前模块下的方法名称
    	foreach ($module_list as $k => $v){
    		$where1['mo_id']  = $v['mo_id'];
    		$module_list[$k]['function_list'] = $this->Function->selectFunction($where1,'','');
    	}
        $this->assign('active',1);
    	$this->assign('project_info',$project_info);
    	$this->assign('module_list',$module_list);
		$this->display();
	}

	/**
	 * 增加模块
	 */
	public function addModule(){
        if(session('M_TYPE') == 2){
            //客户想添加模块
            if($_POST['p_id'] == 22){
                $this->error('您没有添加权限');
            }
        }
		$data['p_id'] = $_POST['p_id'];
		$data['mo_name'] = $_POST['mo_name'];
		$data['mo_e_name'] = $_POST['mo_e_name'];
		$id = $this->Module->addModule($data);
		if($id){
			$moname = $_POST['mo_name'];
			$mo_e_name = $_POST['mo_e_name'];
			$this->ajaxReturn(array('mo_name'=>$moname,'mo_e_name'=>$mo_e_name,'mo_id'=>$id),'JSON');
		}
	}


	/**
	 * 删除模块
	 */

	public function deleteModule(){
        if(session('M_TYPE') == 2){
            //客户想删除模块
            $p_id = $this->Module->where(array('mo_id'=>$_POST['mo_id']))->getField('p_id');
            if($p_id == 22){
                $this->error('您没有删除权限');
            }
        }
		$where['mo_id'] = $_POST['mo_id'];
		$id = $this->Module->deleteModule($where);
		if($id){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}

	/**
	 * 删除方法
	 */

	public function deleteFunction(){
        if(session('M_TYPE') == 2){
            //客户想删除方法
            $mo_id = $this->Function->where(array('f_id'=>$_POST['f_id']))->getField('mo_id');
            $p_id = $this->Module->where(array('mo_id'=>$mo_id))->getField('p_id');
            if($p_id == 22){
                $this->error('您没有删除权限');
            }
        }
		$where['f_id'] = $_POST['f_id'];
		$id = $this->Function->deleteFunction($where);
		if($id){
			$this->success('删除成功');
		}else{
			$this->success('删除失败');
		}
	}


	/**
	 * 增加方法
	 */
	public function addFunction(){
        if(session('M_TYPE') == 2){
            //客户想添加方法
            $p_id = $this->Module->where(array('mo_id'=>$_POST['mo_id']))->getField('p_id');
            if($p_id == 22){
                $this->error('您没有添加权限');
            }
        }
		if($_POST['isPa']=='1'){
			$arr[] = array();
			foreach ($_POST['p_name'] as $k => $v) {
				$arr[$k]['p_type'] = $_POST['p_type'][$k];
				$arr[$k]['p_name'] = $_POST['p_name'][$k];
				$arr[$k]['p_desc'] = $_POST['p_desc'][$k];
				$arr[$k]['p_is_null'] = $_POST['p_is_null'][$k];
                $arr[$k]['default_val'] = $_POST['default_val'][$k];
			}
			$arr_str = serialize($arr);
			$data['f_pa'] =$arr_str;
		}
		$data['mo_id'] = $_POST['mo_id'];
		$data['f_zh_name'] = $_POST['f_zh_name'];
		$data['f_name'] = $_POST['f_name'];
		$data['f_url'] = $_POST['f_url'];
		$data['f_type'] = $_POST['f_type'];
		$data['f_desc'] = $_POST['f_desc'];
		$data['f_success'] = $_POST['f_success'];
		//$data['f_error'] = $_POST['f_error'];
		$data['ctime'] = time();
		$id = $this->Function->addFunction($data);
		if($id){
			$this->success('添加成功');
		}else{
			$this->error("添加失败");
		}
	}

}